﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DBO;

namespace CustomControls.Complex
{
    public partial class TroopList : UserControl
    {
        public TroopList()
        {
            InitializeComponent();
            _controls = new List<TroopControl>();
        }

        public event EventHandler onBuy;
        public event EventHandler onSell;
        public event EventHandler onSelect;

        public void AddTroop(string Troop)
        {
            TroopControl ctrl = new TroopControl();
            ctrl.Troop = Troop;

            GetTroopControls().Add(ctrl);
            ctrl.onClick += new EventHandler(ctrl_onClick);
            ctrl.onBuy +=new EventHandler(ctrl_onBuy);
            ctrl.onSell += new EventHandler(ctrl_onSell);
            ctrl.CanAdd = true;

            Panel.Controls.Add(ctrl);
        }

        void ctrl_onSell(object sender, EventArgs e)
        {
            onSell.Invoke(sender, e);
        }

        void ctrl_onBuy(object sender, EventArgs e)
        {
            onBuy.Invoke(sender, e);
        }

        void ctrl_onClick(object sender, EventArgs e)
        {
            onSelect.Invoke(sender, e);
            TroopControl tc = (TroopControl)sender;
            foreach (TroopControl ctrl in GetTroopControls())
                ctrl.ToggleSelected(Object.ReferenceEquals(tc, ctrl));
        }

        void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
        {
        }


        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        public List<TroopControl> GetTroopControls()
        {
            return _controls;
        }
        private List<TroopControl> _controls;

        private void Panel_Paint(object sender, PaintEventArgs e)
        {

        }


        public bool ConsumeTroop(string type)
        {
            TroopControl ctrl = findControlTroop(type);

            ctrl.TroopNumber--;

            if (ctrl.TroopNumber <= 0)
            {
                foreach (TroopControl c in _controls)
                {
                    c.ToggleSelected(false);
                }

                return true;
            }
            return false;
        }

        public bool IsNegative()
        {
            foreach (TroopControl item in _controls)
            {
                if (item.IsNegative())
                    return true;
            }
            return false;
        }

        public void ReplenishTroop(string type)
        {
            TroopControl ctrl = findControlTroop(type);

            ctrl.TroopNumber++;
        }

        private TroopControl findControlTroop(string type)
        {
            foreach (TroopControl ctrl in GetTroopControls())
            {
                if (ctrl.Troop == type)
                    return ctrl;
            }
            return null;
        }
    }
}
